package user_assets_usdt

import (
	"context"
	"strconv"

	"gitee.com/liuxuezhan/ar-platform/admin-api/internal/svc"
	"gitee.com/liuxuezhan/ar-platform/admin-api/internal/types"
	"gitee.com/liuxuezhan/ar-platform/admin-api/pkg/emun"
	xerrors "gitee.com/liuxuezhan/ar-platform/admin-api/pkg/xerrors/assets"
	"gitee.com/liuxuezhan/ar-platform/rpc-client/assetsClient/rpc"
	"github.com/jinzhu/copier"

	"github.com/zeromicro/go-zero/core/logx"
)

type UserUsdtWalletListPageLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

// 分页查询
func NewUserUsdtWalletListPageLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UserUsdtWalletListPageLogic {
	return &UserUsdtWalletListPageLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *UserUsdtWalletListPageLogic) UserUsdtWalletListPage(req *types.UserUsdtWalletsPageRequest) (resp *types.UserUsdtWalletsPageResponse, err error) {
	var usdtType string
	if req.Param.UsdtType == 0 {
		usdtType = ""
	} else {
		usdtType = strconv.FormatInt(req.Param.UsdtType, 10)
	}
	page, err := l.svcCtx.AssetsRpc.UserChannel.UserUsdtWalletsPage(l.ctx, &rpc.UserUsdtWalletsPageRequest{
		Param: &rpc.UserUsdtWalletsInfoRequest{
			UserId:   req.Param.UserID,
			UsdtType: usdtType,
			Address:  req.Param.Address,
		},
		Page: &rpc.PageRequest{
			PageNo:   int64(req.Page.PageNo),
			PageSize: int64(req.Page.PageSize),
		},
	})
	response := types.UserUsdtWalletsPageResponse{}
	if err != nil {
		return &response, err
	}
	err = copier.Copy(&response, page)
	for i := 0; i < len(page.DataList); i++ {
		t := page.DataList[i].UsdtType
		var key int64
		if t != "" {
			key, err = strconv.ParseInt(t, 10, 64)
		}
		if err != nil {
			return &response, xerrors.ErrParse
		}
		response.DataList[i].UsdtType = emun.GetUserUsdtWalletNameByType(int(key))
	}

	if err != nil {
		return nil, xerrors.ErrDataConvert
	}
	return &response, nil
}
